package no.ntnu.fp.gui.login;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;

import no.ntnu.fp.client.Client;

@SuppressWarnings("serial")
public class SettingsDialog extends JDialog {
	private JTextField localHostname, serverHostname;
	private JSpinner localPortSpinner, serverPortSpinner;
	
	private static SettingsDialog instance;
	public static void createInstance(JDialog parent){
		if(instance != null){
			throw new IllegalStateException("Instance of SettingsDialog allready exist!");
		}else{
			instance = new SettingsDialog(parent);
		}
	}
	public static SettingsDialog getInstance(){
		if(instance == null){
			throw new IllegalStateException("No instance of SettingsDialog exist!");
		}else{
			return instance;
		}
	}
	
	private SettingsDialog(JDialog parent){
		super(parent, true);
		this.getContentPane().setLayout(new GridBagLayout());
		this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(2, 5, 2, 5);
		//Setup hostname localhost
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridy = 0;
		c.gridx = 0;
		this.add(new JLabel("Local hostname:"), c);
		localHostname = new JTextField("localhost");
		c.gridwidth = 2;
		c.gridx = 1;
		this.add(localHostname, c);
		
		//Port number
		c.gridy = 1;
		c.gridx = 0;
		c.gridwidth = 1;
		this.add(new JLabel("Local portnumber:"), c);
		SpinnerModel localPortModel = new SpinnerNumberModel(Client.getPortNumber(), 1000, 100000, 1);
		localPortSpinner = new JSpinner(localPortModel);
		c.gridx = 1;
		c.gridwidth = 2;
		this.add(localPortSpinner, c);
		
		//Server hostname
		c.gridy = 2;
		c.gridx = 0;
		c.gridwidth = 1;
		this.add(new JLabel("Server hostname:"), c);
		serverHostname = new JTextField();
		try {
			serverHostname.setText(InetAddress.getLocalHost().getHostAddress());
		} catch (UnknownHostException e) {
			serverHostname.setText(Client.getHOST_NAME());
		}
		c.gridx = 1;
		c.gridwidth = 2;
		this.add(serverHostname, c);
		
		//Server port
		c.gridx = 0;
		c.gridy = 3;
		c.gridwidth = 1;
		this.add(new JLabel("Server portnumber:"), c);
		SpinnerModel serverPortModel = new SpinnerNumberModel(Client.getServerPort(), 1000, 100000, 1);
		serverPortSpinner = new JSpinner(serverPortModel);
		c.gridx = 1;
		c.gridwidth = 2;
		this.add(serverPortSpinner, c);
		
		c.gridy = 4;
		c.gridx = 1;
		c.gridwidth = 2;
		JButton store = new JButton("Lagre");
		store.addActionListener(new StoreButtonAction());
		this.add(store, c);
		
		this.pack();
		this.setVisible(false);
	}
	
	public void show(Component parent){
		this.setLocationRelativeTo(parent);
		this.setVisible(true);
	}
	
	private final class StoreButtonAction implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			Client.setHOST_NAME(serverHostname.getText());
			Client.setPORT_NUMBER((Integer) localPortSpinner.getValue());
			Client.setSERVER_PORT((Integer) serverPortSpinner.getValue());
			setVisible(false);
		}
	}
}
